﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace MakeBorderless
{
    public class SettingsManager
    {
        #region Variables

        protected string _path = "";
        protected string _fileName = "";
        protected object _settings = new object();

        #endregion Variables

        #region Properties

        public string SavePath { get; set; }

        public string FileName { get; set; }

        #endregion Properties

        #region Constructors

        public SettingsManager()
        {
        }

        public SettingsManager(string savePath, string fileName, object settings)
        {
            _path = savePath;
            _fileName = fileName;
            _settings = settings;
        }

        #endregion Constructors

        #region Public Functions

        public object Load()
        {
            if (_path != "" || _fileName != "")
            {
                return ReadSettings(Path.Combine(_path, _fileName), _settings.GetType());
            }
            return null;
        }

        public object Load(string path, string fileName)
        {
            if (path != "" || fileName != "")
            {
                return ReadSettings(Path.Combine(path, fileName), _settings.GetType());
            }
            return null;
        }

        public object Load(string path, string fileName, Type type)
        {
            if (path != "" || fileName != "")
            {
                return ReadSettings(Path.Combine(path, fileName), type);
            }
            return null;
        }

        public void Save()
        {
            if (_path != "" || _fileName != "")
            {
                WriteSettings(Path.Combine(_path, _fileName), _settings);
            }
        }

        public void Save(string path, string fileName)
        {
            if (path != "" || fileName != "")
            {
                WriteSettings(Path.Combine(path, fileName), _settings);
            }
        }

        #endregion Public Functions

        #region Private Functions

        private object ReadSettings(string fileName, Type type)
        {
            if (File.Exists(fileName))
            {
                FileStream f = new FileStream(fileName, FileMode.Open);
                try
                {
                    XmlSerializer x = new XmlSerializer(type);
                    object obj = x.Deserialize(f);
                    f.Close();
                    return obj;
                }
                catch
                {
                    f.Close();
                    File.Delete(fileName);
                    return null;
                }
            }
            return null;
        }

        private void WriteSettings(string fileName, object settings)
        {
            if (!File.Exists(fileName))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fileName));
            }
            FileStream f = new FileStream(fileName, FileMode.Create);
            XmlSerializer x = new XmlSerializer(settings.GetType());
            x.Serialize(f, settings);
            f.Close();
        }

        #endregion Private Functions
    }
}